package com.app.web.front.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.app.web.BaseModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * type (1:话题回复评论, 2: 消息评论, 3: 博客评论)
 */
public class Comment extends BaseModel<Comment>{
	
	private static final long serialVersionUID = 7615180955823818222L;
	/**常量**/
	public static final int TYPE_REPLY = 1;
	public static final int TYPE_MESSAGE = 2;
	public static final int TYPE_BLOG = 3;
	
	public static final Comment dao = new Comment();
	
	public Record getComment(int id){
		return Db.findFirst("select a.*, b.user_name as sender_name, c.user_name as receiver_name, "+
							"b.pic_url as sender_pic_path from comment a "+
							"left outer join user b on a.sender_id = b.id "+
							"left outer join user c on a.receiver_id = c.id "+
							"where a.id = ?", id);
	}
	public List<Record> findByTypeId(int typeId, int type,int userId){
		List<Record> records=null;
		if(type==3){//与我相关
			String sql = "SELECT "+
							"a.id,a.type_id,a.type,a.sender_id,a.receiver_id,a.content,a.create_time,"+
							"b.id AS send_id,b.user_name AS send_name,b.pic_url AS sender_pic,"+
							"c.id AS receiv_id,c.user_name AS receiv_name,c.pic_url AS receiv_pic "+
						" FROM COMMENT a "+
							" INNER JOIN USER b ON  a.sender_id = b.id "+
							" INNER JOIN USER c ON  a.receiver_id = c.id "+
							" WHERE a.type_id = ? AND (a.sender_id = ? OR a.receiver_id = ?) order by a.id";
			
			records = Db.find(sql, new Object[]{typeId,userId,userId});
		}else{//好友评论

			records = Db.find("select a.*, b.user_name as sender_name, c.user_name as receiver_name,"+
					"b.pic_url as sender_pic from comment a "+
					"left outer join user b on a.sender_id = b.id "+
					"left outer join user c on a.receiver_id = c.id "+
					"where a.type_id = ? and a.type = ? order by a.id", typeId, type);
		}
		return this.getCommTree(records);
	}
	
	
	private List<Record> getCommTree(List<Record> list){
		Map<Integer, Record> map = new HashMap<Integer, Record>();
		for (Record record : list) {
			map.put(record.getInt("id"), record);
		}
		
		List<Record> records = new ArrayList<Record>();
		Record temp ;
		for (Record record : map.values()) {
			temp = map.get(record.getInt("pid"));
			if(temp == null){
				records.add(record);
			}else{
				
				List<Record> subComments = temp.get("subComments");
				if(subComments == null){
					subComments = new ArrayList<Record>();
					temp.set("subComments", subComments);
				}
				subComments.add(record);
			}
		}
		
		
		// 按照 id 排序
		Collections.sort(records, this.getComparator());
		
		for (Record record : records) {
			List<Record> subComments = record.get("subComments");
			if(subComments != null){
				Collections.sort(subComments, this.getComparator());
			}
		}
		
		return records;
	}
	
	public  Comparator<Record> getComparator(){
		return new Comparator<Record>() {

			@Override
			public int compare(Record o1, Record o2) {
				// TODO Auto-generated method stub
				return o1.getInt("id").compareTo(o2.getInt("id"));
			}
		};
	}
}
